package com.hnucm.cooklife.ui.activity;

import android.app.Activity;
import android.content.Context;
import android.os.Build;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;
import androidx.core.graphics.ColorUtils;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.hnucm.cooklife.R;
import com.hnucm.cooklife.adapter.MsgAdapter;
import com.hnucm.cooklife.base.BaseActivity;
import com.hnucm.cooklife.base.BasePresenter;
import com.hnucm.cooklife.base.Utils;
import com.hnucm.cooklife.bean.db.Msg;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;

public class ChatActivity extends AppCompatActivity  {

    private List<Msg> mMsgList = new ArrayList<>();

    private Unbinder mUnbinder;
    @BindView(R.id.input_text)
    EditText inputText;

    @BindView(R.id.send)
    Button send;

    @BindView(R.id.msg_recycler_view)
    RecyclerView mRecyclerView;

    @BindView(R.id.chat_toolbar)
    Toolbar mToolbar;

    private MsgAdapter mAdapter;
    private Context mContext;
    private LinearLayoutManager manager;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_chat);
        mUnbinder = ButterKnife.bind(this);
//        init(savedInstanceState);
        mContext = getApplicationContext();
        initMsgs();//初始化数据
        manager = new LinearLayoutManager(mContext,LinearLayoutManager.VERTICAL,false);
        mRecyclerView.setLayoutManager(manager);
        mAdapter = new MsgAdapter(mMsgList);
        mRecyclerView.setAdapter(mAdapter);
//        initAdapter();
        initToolbar();
        initStatusBar();
    }

    private void init(Bundle savedInstanceState) {

    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        mUnbinder.unbind();
    }

    private void initToolbar() {
        mToolbar.setBackgroundColor(Utils.getColor(mContext));
        setSupportActionBar(mToolbar);
        if (getSupportActionBar() != null) {
            getSupportActionBar().setHomeAsUpIndicator(R.drawable.ic_back);
            getSupportActionBar().setDisplayHomeAsUpEnabled(true);
            getSupportActionBar().setDisplayShowTitleEnabled(true);
        }
        mToolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }

    private void initStatusBar() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            getWindow().setStatusBarColor(Utils.getColor(mContext));
        }
        if (ColorUtils.calculateLuminance(Utils.getColor(mContext)) >= 0.5) {
            // 设置状态栏中字体的颜色为黑色
            getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);
        } else {
            // 跟随系统
            getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_STABLE);
        }
    }

    private void initMsgs() {
        Msg hello_guy = new Msg("您好，下厨房：煎饼果子为您服务！"+"\n"+"请问有什么可以帮到你的呢？", Msg.TYPE_RECEIVE);
        mMsgList.add(hello_guy);
    }

    private void initAdapter() {

    }

    @OnClick(R.id.send)
    public void onClick(){
        String content = inputText.getText().toString();
        if(! "".equals(content)){
            Msg msg = new Msg(content, Msg.TYPE_SENT);
            mMsgList.add(msg);

            if(content.contains("地址")){
                Msg loc = new Msg("亲，我们默认从浙江宁波发货哦", Msg.TYPE_RECEIVE);
                mMsgList.add(loc);
            } else if(content.contains("快递")){
                Msg expres = new Msg("亲，我们默认圆通快递，不支持指定快递", Msg.TYPE_RECEIVE);
                mMsgList.add(expres);
            }
            mAdapter.notifyDataSetChanged();
            //将recyclerview定位到最后一行
            mRecyclerView.scrollToPosition(mMsgList.size() - 1);
            inputText.setText("");//清空输入框中的内容
        }
    }

}
